package br.com.persistence;

import java.util.List;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.SystemClock;
import br.com.model.Pergunta;

import org.droidpersistence.dao.TableDefinition;
import org.droidpersistence.dao.DroidDao;
import org.droidpersistence.model.InterfaceBean;

public class DaoFactory<T> {

	private Context context;
	private SQLiteDatabase database;
	private DroidDao<T, Long> dao;
	
	/**
	 * Lista com as tables Definition de todos os Bean
	 * esta lista vai ser usada nos metos onUpgrade e onCreate
	 */
	@SuppressWarnings("rawtypes")
	private static final TableDefinition[] tableDefinitionList = { 
		new TableDefinition<Pergunta>(Pergunta.class) {}
		};
	
	public DaoFactory(Context context, Class<T> clazz){
		setContext(context);
		SQLiteOpenHelper openHelper = new OpenHelper(context, "BEANDATABASE", null, 2);
		setDatabase(openHelper.getWritableDatabase());
		
		this.setDao(new DroidDao<T, Long>(clazz, database));
	}
	
	private void openDb(){
		if(! getDatabase().isOpen()){
			setDatabase(SQLiteDatabase.openDatabase(Environment.getDataDirectory() + "/data/br.com.beandatabase/databases/beandatabase.db", null, SQLiteDatabase.OPEN_READWRITE));
		}
	}
	
	private void closeDb() {
		if (getDatabase().isOpen()) {
			getDatabase().close();
	    }
	}

	private void resetDb() {      
		closeDb();
	    SystemClock.sleep(500);
	    openDb();
	}
	
	public T get(Long id){		
		return getDao().get(id);
	}

	public T get(InterfaceBean bean){		
		return getDao().get(bean.getId());
	}
	
	public List<T> getAll(){
		return getDao().getAll();
	}
	
	
	public boolean delete(Long id){
		
		getDatabase().beginTransaction();
		boolean result = getDao().delete(id);
		getDatabase().setTransactionSuccessful();
		getDatabase().endTransaction();
		return result;
	}
	
	
	public long save(T bean){
		long result = 0;		
		try {
			getDatabase().beginTransaction();
			
			result = getDao().save(bean);
			
			getDatabase().setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		}		
		getDatabase().endTransaction();
		return result;
	}
	
	
	@SuppressWarnings("unchecked")
	public boolean update(InterfaceBean bean){
		boolean result = false;
		try {
		getDatabase().beginTransaction();
		
		getDao().update((T) bean, bean.getId());
		
		getDatabase().setTransactionSuccessful();
		result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}		
		getDatabase().endTransaction();
		return result;
	}
	
	public Context getContext() {
		return context;
	}
	public void setContext(Context context) {
		this.context = context;
	}
	public SQLiteDatabase getDatabase() {
		return database;
	}
	public void setDatabase(SQLiteDatabase database) {
		this.database = database;
	}

	public DroidDao<T, Long> getDao() {
		return dao;
	}

	private void setDao(DroidDao<T, Long> dao) {
		this.dao = dao;
	}

	public static void onCreate(SQLiteDatabase db){
		for (TableDefinition table : tableDefinitionList) {
			try {
				table.onCreate(db);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
		for (TableDefinition table : tableDefinitionList) {
			try {
				table.onUpgrade(db, oldVersion, newVersion);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
